﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 仪表识别2
{
    public class Vector3
    {
        private float x;
        private float y;
        private float z;
        private const float E = 0.0000001f;

        public float X
        {
            set { x = value; }
            get { return x; }
        }

        public float Y
        {
            set { y = value; }
            get { return y; }
        }

        public float Z
        {
            set { z = value; }
            get { return z; }
        }

        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector3(Vector3 vct)
        {
            this.x = vct.x;
            this.y = vct.y;
            this.z = vct.z;
        }

        //向量加法
        public static Vector3 operator +(Vector3 a, Vector3 b)
        {
            Vector3 result = new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
            return result;
        }

        //向量减法
        public static Vector3 operator -(Vector3 a, Vector3 b)
        {
            Vector3 result = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
            return result;
        }

        //向量除以一个数
        public static Vector3 operator /(Vector3 a, float b)
        {
            if (b != 0)
            {
                return new Vector3(a.x / b, a.y / b, a.z / b);
            }
            else
            {
                return new Vector3(0, 0, 0);
            }
        }

        // 左乘一个数
        public static Vector3 operator *(float a, Vector3 b)
        {
            return new Vector3(a * b.x, a * b.y, a * b.z);
        }

        // 右乘一个数
        public static Vector3 operator *(Vector3 a, float b)
        {
            return new Vector3(a.x * b, a.y * b, a.z * b);
        }

        // 向量的点乘
        public static float operator *(Vector3 a, Vector3 b)
        {
            return a.x * b.x + a.y * b.y + a.z * b.z;
        }

        // 判断两个向量是否相等
        public static bool operator ==(Vector3 a, Vector3 b)
        {
            if (Math.Abs(a.x - b.x) < E && Math.Abs(a.y - b.y) < E && Math.Abs(a.z - b.z) < E)
            {
                return true;
            }
            else
                return false;
        }

        // 判断两个向量不等
        public static bool operator !=(Vector3 a, Vector3 b)
        {
            return !(a == b);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return base.ToString();
        }

        // 向量叉积
        public static Vector3 Cross(Vector3 a, Vector3 b)
        {
            return new Vector3(a.y * b.z - a.z * b.y,
                                a.z * b.x - a.x * b.z,
                                a.x * b.y - a.y * b.x);
        }

        //向量的模
        public static float Magnitude(Vector3 a)
        {
            return (float)Math.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
        }

        // 单位化向量

        public static Vector3 Normalize(Vector3 a)
        {
            float magnitude = Magnitude(a);
            return new Vector3(a.x / magnitude, a.y / magnitude, a.z / magnitude);
        }
    }
}
